package de.koller.worlders;

import de.koller.oceanFramework.tree.Child;
import de.koller.worlders.engine.SyncClear;
import de.koller.worlders.engine.WInputGL;
import de.koller.worlders.engine.debug.Debug;
import de.koller.worlders.engine.entity.EntityContainer;
import de.koller.worlders.engine.entity.WorldersIO;
import de.koller.worlders.engine.tiles.TileMap;
import de.koller.worlders.engine.tiles.background.Background;
import de.koller.worlders.engine.tiles.background.NightSky;
import de.koller.worlders.engine.util.BufferCollection;
import de.koller.worlders.gameplay.GameplayPlayer;
import de.koller.worlders.gameplay.Progress;
import de.koller.worlders.gameplay.gui.GUI;
import de.koller.worlders.gameplay.inventory.Inventory;
import de.koller.worlders.render.RenderRootGL;
import de.koller.worlders.render.VisualMap;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

public class WorldersRoot extends WSea {
	public static int D_WIDTH, D_HEIGHT;
		
	private long time = 0;

	private final User user;

	@Child public final VisualMap vmap = new VisualMap();
	
	@Child private final BufferCollection buffers = new BufferCollection();
	@Child private final WInputGL input = new WInputGL();
	@Child private final WorldersIO wio = new WorldersIO();

	@Child private final TileMap tileMap = new TileMap();
	@Child private final EntityContainer entityContainer = new EntityContainer();
	@Child public final Inventory inventory = new Inventory();
	@Child public final SyncClear syncClear = new SyncClear();
	//@Child public final EnvRoot environment = new EnvRoot();
	@Child public final GameplayPlayer gamePlayer = new GameplayPlayer();
	
	@Child private final GUI gui = new GUI();

	@Child private final Debug debug = new Debug();
	@Child public final Background background = new NightSky();
	
	@Child public final RenderRootGL rendering = new RenderRootGL();
	private boolean paused = false;
	
	public WorldersRoot( User user ) {
		this.user = user;
		user.loadMapName();
	}

	public WorldersRoot() {
		this( new User() );
	}

	public void tileChanged( int tx, int ty ) {
		//environment.tileChanged( tx, ty );
	}
	
	@Override
	protected void init() {
		super.init();
		
		D_WIDTH = Display.getWidth();
		D_HEIGHT = Display.getHeight();
		System.out.println( "Started game at " + D_WIDTH + "x" + D_HEIGHT );
		
		this.setIgnoreChildRender( true );
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		rendering.renderGame( elapsedTime );
	}
	
	@Override
	protected void postChildInit() {
		super.postChildInit();
		
	}
	
	public void gameReset() {
		this.gamePlayer.playerPoints.reset();
		this.resetNode();
	}
	
	@Override
	protected void release() {
		super.release();
		
		user.endGame( this );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( input.isKeyPressed( Keyboard.KEY_ESCAPE ) ) {
			paused = !paused;
			this.setIgnoreChildUpdate( paused );
		}
		
		if( paused ) {
			//@todo Warning, call to update!
			input.updating( elapsedTime );
			return;
		}
		
		time += elapsedTime;
	}

	@Override
	public void postChildUpdate( int elapsedTime ) {
		super.postChildUpdate( elapsedTime );
		
	}

	public BufferCollection getBuffers() {
		return buffers;
	}

	public WInputGL getInput() {
		return input;
	}

	public long getTime() {
		return time;
	}

	public Debug getDebug() {
		return debug;
	}

	public User getUser() {
		return user;
	}

	public WorldersIO getWio() {
		return wio;
	}

	public GUI getGui() {
		return gui;
	}
	
	public Progress getProgress() {
		return this.gamePlayer.playerPoints.getProgress();
	}

	public EntityContainer getEntityContainer() {
		return entityContainer;
	}

	public TileMap getTileMap() {
		return tileMap;
	}

	public boolean isPaused() {
		return paused;
	}
	
}
